package com.hwl.acl.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hwl.acl.pojo.User;
import com.hwl.acl.service.RoleService;
import com.hwl.acl.service.UserService;
import com.hwl.commonutils.result.RespBean;
import com.hwl.commonutils.utils.AssertUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author long
 * @since 2021-10-16
 */
@Api(tags = "后台管理员管理")
@RestController
@RequestMapping("/admin/acl/user")
public class UserController {

    @Resource
    private UserService userService;
    @Resource
    private RoleService roleService;

    @ApiOperation("获取管理员分页列表")
    @GetMapping("{limit}/{size}")
    public RespBean pageAdmin(@PathVariable Long limit, @PathVariable Long size,
                              @RequestParam(value = "userName", required = false) String userName) {
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(userName)) {
            wrapper.like("username", userName);
        }
        return RespBean.ok().data(userService.page(new Page<>(limit, size), wrapper));
    }

    @ApiOperation("添加管理员")
    @PostMapping("save")
    public RespBean addUser(@RequestBody User user) {
        AssertUtil.isTrue(userService.getUserByName(user.getUsername()) != null, "用户名不得重复！");
        //TODO 密码加密
        if (userService.save(user)) {
            return RespBean.ok().message("新增管理员成功！");
        }
        return RespBean.error().message("新增管理员失败！");
    }

    @ApiOperation("获取管理员信息")
    @GetMapping("get/{id}")
    public RespBean getById(@PathVariable String id) {
        return RespBean.ok().data(userService.getById(id));
    }

    @ApiOperation("更新管理员信息")
    @PutMapping("update")
    public RespBean updateById(@RequestBody User user) {
        User userByName = userService.getUserByName(user.getUsername());
        AssertUtil.isTrue(userByName != null && !userByName.getId().equals(user.getId()), "用户名不得重复!");
        if (userService.updateById(user)) {
            return RespBean.ok().message("更新管理员成功！");
        }
        return RespBean.error().message("更新管理员失败！");
    }

    @ApiOperation("删除管理员")
    @DeleteMapping("remove/{id}")
    public RespBean remove(@PathVariable String id) {
        if (userService.removeById(id)) {
            return RespBean.ok().message("删除管理员成功！");
        }
        return RespBean.error().message("删除管理员失败！");
    }

    @ApiOperation("批量删除管理员")
    @DeleteMapping("batchRemove")
    public RespBean batchRemove(@RequestBody List<String> idList) {
        if (userService.removeByIds(idList)) {
            return RespBean.ok().message("批量删除管理员成功！");
        }
        return RespBean.error().message("批量删除管理员失败！");
    }

    @ApiOperation("查看该管理员所拥有的角色")
    @GetMapping("toAssign/{userId}")
    public RespBean toAssign(@PathVariable String userId) {
        return RespBean.ok().data(roleService.findRoleByUserId(userId));
    }

    @ApiOperation("为管理员分配角色")
    @PostMapping("doAssign")
    public RespBean doAssign(String userId, String[] roleIds) {
        if (roleIds.length == roleService.assigningRoles(userId, roleIds)) {
            return RespBean.ok().message("分配角色成功！");
        }
        return RespBean.error().message("分配角色失败！");
    }

}

